package edu.udo.sopra10.chaturaji.util;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Utility methods for working with complex collection data types.
 * 
 * @author Simon Dierl
 */
public class CollectionUtil {
	/**
	 * Add an entry to a possibly {@code null} {@link List} stored in a
	 * {@link Map}.
	 * 
	 * @param <K>
	 *            the key type for the map.
	 * @param <V>
	 *            the values stores in the lists in the map.
	 * @param map
	 *            the map containing the lists, must not be {@code null}.
	 * @param key
	 *            the key under which to file the entry.
	 * @param value
	 *            the value to store in the list in the map.
	 */
	public static <K, V> void addToListMap(Map<K, List<V>> map, K key, V value) {
		List<V> valueList = map.get(key);
		if (valueList == null) {
			valueList = new LinkedList<V>();
			map.put(key, valueList);
		}
		valueList.add(value);
	}
}
